package tr.gov.tubitak.bilgem.esya.certselector;/*
 * Created by JFormDesigner on Tue Nov 15 11:49:39 EET 2011
 */

import javax.swing.*;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.ResourceBundle;

/**
 * @author Haydar Karabas
 */
public class EPinPad extends JPanel implements ActionListener {
    public boolean isShuffleNumbers() {
        return cBoxShuffle.isSelected();
    }

    public void setShuffleNumbers(boolean shuffleNumbers) {
        cBoxShuffle.setSelected(shuffleNumbers);
    }

    public void clearFields()
    {
        passwordField.setText("");
    }

    void resetKeyValues()
    {
        if(cBoxShuffle.isSelected())
        {
            ArrayList<Integer> list = new ArrayList<Integer>();
            Random r = new Random();
            for (int i = 0; i < 10; i++) {
                list.add(i);
            }
            ArrayList<JButton> components = new ArrayList<JButton>();
            components.add(eBtn0);
            components.add(eBtn1);
            components.add(eBtn2);
            components.add(eBtn3);
            components.add(eBtn4);
            components.add(eBtn5);
            components.add(eBtn6);
            components.add(eBtn7);
            components.add(eBtn8);
            components.add(eBtn9);
            for (JButton component : components) {
                    int tempIndex = r.nextInt(list.size());
                    Integer tempValue = list.get(tempIndex);
                    component.setText(tempValue.toString());
                    list.remove(tempIndex);
            }
        }
        else
        {
            eBtn1.setText("1");
            eBtn2.setText("2");
            eBtn3.setText("3");
            eBtn4.setText("4");
            eBtn5.setText("5");
            eBtn6.setText("6");
            eBtn7.setText("7");
            eBtn8.setText("8");
            eBtn9.setText("9");
            eBtn0.setText("0");
        }
    }
    public EPinPad() {
        initComponents();
        eBtn0.addActionListener(this);
        eBtn1.addActionListener(this);
        eBtn2.addActionListener(this);
        eBtn3.addActionListener(this);
        eBtn4.addActionListener(this);
        eBtn5.addActionListener(this);
        eBtn6.addActionListener(this);
        eBtn7.addActionListener(this);
        eBtn8.addActionListener(this);
        eBtn9.addActionListener(this);
    }

    public void addPinFieldListener(DocumentListener documentListener)
    {
        passwordField.getDocument().addDocumentListener(documentListener);
    }

    public String getPin()
    {
        String pin = String.valueOf(passwordField.getPassword());
        return pin;
    }

    private void eBtnSilActionPerformed(ActionEvent e) {
        char[] password = passwordField.getPassword();
        if (password.length > 0) {
                    String pin = String.valueOf(password);
                    passwordField.setText(pin.substring(0, pin.length() - 1));
                }
    }

     public void actionPerformed(ActionEvent e) {
        String pin = String.valueOf(passwordField.getPassword());
        pin += e.getActionCommand();
        passwordField.setText(pin);
        resetKeyValues();
    }

    private void cBoxShuffleActionPerformed(ActionEvent e) {
        resetKeyValues();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel3 = new JPanel();
        xLblParola = new JLabel();
        passwordField = new JPasswordField();
        panel1 = new JPanel();
        eBtn1 = new JButton();
        eBtn2 = new JButton();
        eBtn3 = new JButton();
        eBtn4 = new JButton();
        eBtn5 = new JButton();
        eBtn6 = new JButton();
        eBtn7 = new JButton();
        eBtn8 = new JButton();
        eBtn9 = new JButton();
        eBtn0 = new JButton();
        eBtnSil = new JButton();
        panel2 = new JPanel();
        cBoxShuffle = new JCheckBox();

        //======== this ========
        setBorder(null);
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {0, 174, 0, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {65, 140, 35, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0E-4};

        //======== panel3 ========
        {
            panel3.setLayout(new GridBagLayout());
            ((GridBagLayout)panel3.getLayout()).columnWidths = new int[] {10, 20, 161, 10, 0};
            ((GridBagLayout)panel3.getLayout()).rowHeights = new int[] {27, 30, 0};
            ((GridBagLayout)panel3.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};
            ((GridBagLayout)panel3.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0E-4};

            //---- xLblParola ----
            xLblParola.setText(CertSelectorBundle.getString("EPinPad.Password"));
            xLblParola.setFont(new Font("Tahoma", Font.BOLD, 12));
            panel3.add(xLblParola, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- passwordField ----
            passwordField.setHorizontalAlignment(SwingConstants.CENTER);
            panel3.add(passwordField, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        }
        add(panel3, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

        //======== panel1 ========
        {
            panel1.setLayout(new GridBagLayout());
            ((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {30, 50, 5, 50, 5, 50, 0};
            ((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {30, 5, 30, 5, 30, 5, 30, 0};
            ((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
            ((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

            //---- eBtn1 ----
            eBtn1.setText("1");
            panel1.add(eBtn1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn2 ----
            eBtn2.setText("2");
            panel1.add(eBtn2, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn3 ----
            eBtn3.setText("3");
            panel1.add(eBtn3, new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn4 ----
            eBtn4.setText("4");
            panel1.add(eBtn4, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn5 ----
            eBtn5.setText("5");
            panel1.add(eBtn5, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn6 ----
            eBtn6.setText("6");
            panel1.add(eBtn6, new GridBagConstraints(5, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn7 ----
            eBtn7.setText("7");
            panel1.add(eBtn7, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn8 ----
            eBtn8.setText("8");
            panel1.add(eBtn8, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn9 ----
            eBtn9.setText("9");
            panel1.add(eBtn9, new GridBagConstraints(5, 4, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtn0 ----
            eBtn0.setText("0");
            panel1.add(eBtn0, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

            //---- eBtnSil ----
            eBtnSil.setText(CertSelectorBundle.getString("EPinPad.Erase"));
            eBtnSil.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    eBtnSilActionPerformed(e);
                }
            });
            panel1.add(eBtnSil, new GridBagConstraints(3, 6, 3, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        }
        add(panel1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

        //======== panel2 ========
        {
            panel2.setLayout(new GridBagLayout());
            ((GridBagLayout)panel2.getLayout()).columnWidths = new int[] {30, 0, 0};
            ((GridBagLayout)panel2.getLayout()).rowHeights = new int[] {20, 10, 0};
            ((GridBagLayout)panel2.getLayout()).columnWeights = new double[] {0.0, 1.0, 1.0E-4};
            ((GridBagLayout)panel2.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0E-4};

            //---- cBoxShuffle ----
            cBoxShuffle.setText(CertSelectorBundle.getString("EPinPad.ShuffleNumbers"));
            cBoxShuffle.setFont(new Font("Tahoma", Font.PLAIN, 12));
            cBoxShuffle.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cBoxShuffleActionPerformed(e);
                }
            });
            panel2.add(cBoxShuffle, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        }
        add(panel2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel3;
    private JLabel xLblParola;
    private JPasswordField passwordField;
    private JPanel panel1;
    private JButton eBtn1;
    private JButton eBtn2;
    private JButton eBtn3;
    private JButton eBtn4;
    private JButton eBtn5;
    private JButton eBtn6;
    private JButton eBtn7;
    private JButton eBtn8;
    private JButton eBtn9;
    private JButton eBtn0;
    private JButton eBtnSil;
    private JPanel panel2;
    private JCheckBox cBoxShuffle;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
